
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import os
import cgi




'''
    generic day object to get the events for a particular day
    from the DB
    need to pass day as a parameter somehow
     - possibly by POSTing some information when a user clicks on the
     image of a particular day. eg: user clicks "monday", monday is POSTED,
     we use that to render the events for monday.
'''

'''
        figure out how to get POST data in Python
        use POST data to fill these in
    '''
    
class EventViewBuilder:
    
    """Event Objects will be passed in the form of Arrays of event objects
     [Event 1, Event 2, Event 3] where 
     Event 1 etc = [Event Name, EventLink, Location, Event Pic URL, AgeLimit, VIP, Start_time, Date, Promoted]
     """
        
    
        def print_header(self):
            header = """
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

             "http://www.w3.org/TR/html4/loose.dtd"> 
        
            <html>

            <link rel="stylesheet" type="text/css" href="stylesheets/eventinfo.css" />
            <head>

            </head>"""
            return header
        
        def print_footer(self):
            
            footer = """</body>

            </html>"""
            return footer
        
        def print_header_details(self,name,location,link):
            result = ""
            result+= "<p> Event Name: " +name+ "</p>"
            result+= "<p> Event Location: " +location+ "</p>"
            result+= "<p> Event Link: " +"<a href =" +'"' +put_into_gmaps_url(link)+'"' +"</a>" +"</p>"
            
            return result;
        
        def print_body(self,event):
            body = ""
            body_title = "<body>"

            body+=body_title
            body+=""" <center> <div id= "stylized" class = Event_Container>"""
            body+= self.print_club_icon(event["profile_link"])
            body+= self.print_header_details(event["name"], event["location"], event["link"])
            body+="<div class = divider></div>"
            body+= self.print_event_details(event["date"], event["start_time"], event["location"])
            body+="<div class = divider></div>"
            body+="""<div class = "container">"""
            body+= self.print_icons(event["age"], event["price"], event["genre"])
            body+= self.print_icons(event["VIP"], event["promoted"], event["day"])
            
            body+="""</div>"""
            body+="""</center>"""
            body += "</body>"
            return body
        
        def print_event_details(self, date, start_time, location):
            result = """<div class ="Event_Details">"""
            result+= "Date: " +date +"<br>" 
            result+= "Start Time: " +start_time +"<br>" 
            result+= "Location: " +location 
            return result
        
            
            
        def print_icons(self, para1, para1,para3):
            icon_container ="""<table id = "daytable_top">"""
            icon_container += "<tr>"
            
            icon_container += """<td><img class="zoom"  src=""" +'"' +para1 +".png"+'"/></td>'
            icon_container += """<td><img class="zoom"  src=""" +'"' +para2 +".png"+'"/></td>'
            icon_container += """<td><img class="zoom"  src=""" +'"' +para3 +".png"+'"/></td>'
            icon_container += "</tr>"    
            icon_container+="""</table>"""
            return icon_container
        
        
        '''
        Instead of day as an argument, why not use  key-value pair
        and make this function generic for days, genres etc etc?
        '''
        def generate(self,events, day):
            
            finalstring=""
            finalstring += self.print_header()
            finalstring += self.print_body(day,events)
            finalstring += self.print_footer()
            return finalstring;
        
        def create_club_icon(self, event, icon):
            url = event["eventlink"]
            profile_pic_link = event["profilepic"]
            icon = "<a href='" + url + "'>" 
            
            icon += """<img class = "zoom" src=""" + profile_pic_link
            icon +=""" alt="">"""
            icon += "</a>"
                
            return icon
        
        
        def print_location(self,url):
            url = self.put_into_gmaps_url(url)
            icon = "<a href='" + url + "'>" 
            icon += "</a>"
                
            return icon
                
        def put_into_gmaps_url(self,addr):
            encoded = ""
            
            for c in addr:
                
                if c == ' ':
                    c = '+'
                    
                encoded += c
            url = "https://maps.google.ie/maps?q=" + encoded
            return url